package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.entity.BankCard;
import org.example.models.R;
import org.example.service.BankCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/finance/account")
public class BankCardController {

    @Autowired
     BankCardService bankCardService;

    @GetMapping()
    public R<Page<BankCard>> page(
            @RequestParam(name="current",defaultValue = "1") int current,
            @RequestParam(name="size",defaultValue = "2") int size,
            @RequestParam(name="account",required = false) String account,
            @RequestParam(name="startOpeingMoney",required = false) Double startOpeingMoney,
            @RequestParam(name="endOpeingMoney",required = false) Double endOpeingMoney
    ){
        QueryWrapper<BankCard> queryWrapper = new QueryWrapper<>();
        if(account != null && account.length()>0){
            queryWrapper.lambda().like(BankCard::getAccount,account);
        }
        if(startOpeingMoney != null){
            queryWrapper.lambda().ge(BankCard::getOpeningAmount,startOpeingMoney);
        }
        if(endOpeingMoney != null){
            queryWrapper.lambda().le(BankCard::getOpeningAmount,endOpeingMoney);
        }
        queryWrapper.lambda().orderByDesc(BankCard::getCreatetime);
        Page<BankCard> page = new Page<>(current, size);
        bankCardService.page(page,queryWrapper);
        return R.success(page);
    }

    @PostMapping("/insert")
    public R insert(@RequestBody BankCard bankCard){
        bankCardService.save(bankCard);
        return R.success();
    }
    @PutMapping("/update")
    public R update(@RequestBody BankCard bankCard){
        bankCardService.updateById(bankCard);
        return R.success();
    }

    @DeleteMapping("/{id}")
    public R delete(@PathVariable("id") String id){
        bankCardService.removeById(id);
        return R.success();
    }
}
